package com.haiyi.software.chat;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import pl.droidsonroids.gif.GifDrawable;
import pl.droidsonroids.gif.GifImageView;
import android.R.integer;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.content.res.Resources.NotFoundException;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.RelativeLayout.LayoutParams;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.haiyi.software.app.ActivityStandardImage;
import com.haiyi.software.app.ImagePagerActivity;
import com.haiyi.software.app.MainApplication;
import com.haiyi.software.app.R;
import com.haiyi.software.dao.ChatDAO;
import com.haiyi.software.entity.EntityMsg;
import com.haiyi.software.entity.EntityMsgGif;
import com.haiyi.software.entity.EntityMsgImage;
import com.haiyi.software.entity.EntityMsgVoice;
import com.haiyi.software.entity.EntityUserPhoto;
import com.haiyi.software.entity.HiChatMessage;
import com.haiyi.software.entity.UserFriend;
import com.haiyi.software.entity.UserInfo;
import com.haiyi.software.tools.L;
import com.haiyi.software.tools.MyTools;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

public class HiChatAdapter extends BaseAdapter {

	private final String TAG = "HiChatAdapter";
	private Context mContext;
	private ArrayList<HiChatMessage> mMessages;
	private DisplayImageOptions options;
	protected ImageLoader imageLoader;

	private Resources res = null;

	private String packetName = null;

	private String str_server = MainApplication.getInstance().getResources()
			.getString(R.string.URL);

	private UserFriend userFriend;

	private UserInfo myInfo = MainApplication.getInstance().getMineUserInfo();

	private String CurrentDateTime = "1970-01-01 00:00:01";

	private MyTools mt;

	public HiChatAdapter(Context context, ArrayList<HiChatMessage> messages,
			UserFriend uFriend) {

		this.mContext = context;
		this.mMessages = messages;
		this.res = context.getResources();
		this.packetName = context.getPackageName();
		this.userFriend = uFriend;

		// 图片控件
		imageLoader = ImageLoader.getInstance();
		options = new DisplayImageOptions.Builder()
				.imageScaleType(ImageScaleType.EXACTLY_STRETCHED)
				.showImageOnLoading(R.drawable.ic_launcher)
				.showImageForEmptyUri(R.drawable.ic_launcher)
				.showImageOnFail(R.drawable.ic_launcher).cacheInMemory(true)
				.cacheOnDisk(true).considerExifParams(true)
				.displayer(new RoundedBitmapDisplayer(10)).build();

		mt = MyTools.getInstance();
	}

	@Override
	public int getCount() {
		return mMessages.size();
	}

	@Override
	public Object getItem(int position) {
		return mMessages.get(position);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		HiChatMessage message = mMessages.get(position);
		CurrentDateTime = position == 0 ? "1970-01-01 00:00:00" : (mMessages
				.get(position == 0 ? 0 : position - 1).getMessage()
				.getDatetime());

		try {
			switch (message.getMessage().getMessageCategory()) {
			case EntityMsg.MESSAGECATEGORY_TEXT:

				convertView = displayTextMessage(position, convertView, parent,
						message);

				break;

			case EntityMsg.MESSAGECATEGORY_GIF:

				convertView = displayGifMessage(position, convertView, parent,
						message);

				break;

			case EntityMsg.MESSAGECATEGORY_VOICE:

				convertView = displayVoiceMessage(position, convertView,
						parent, message);

				break;

			case EntityMsg.MESSAGECATEGORY_IMAGE:

				convertView = displayImageMessage(position, convertView,
						parent, message);

				break;

			}
		} catch (Exception e) {
			// TODO: handle exception
			Log.d(TAG, "内存溢出了吗？" + e.getMessage());
		}

		return convertView;
	}

	private View displayImageMessage(int position, View convertView,
			ViewGroup parent, HiChatMessage message) {

		ViewHolderImageLeft holderLeft = null;
		ViewHolderImageRight holderRight = null;

		int msgtype = message.getWhoseMessage();

		final EntityMsgImage imageMsg = (EntityMsgImage) message.getMessage();

		imageMsg.setContext(mContext);

		switch (msgtype) {
		case HiChatMessage.CHATMESSAGE_MINE:

			if ((convertView == null)
					|| !(convertView.getTag() instanceof ViewHolderImageRight)) {

				convertView = LayoutInflater.from(mContext).inflate(
						R.layout.item_chat_right_image, parent, false);

				holderRight = new ViewHolderImageRight();

				holderRight.img_chatImage = (ImageView) convertView
						.findViewById(R.id.img_chat_image_right);

				holderRight.img_imageView = (ImageView) convertView
						.findViewById(R.id.img_chat_photo_right);

				holderRight.txt_chatDatetime = (TextView) convertView
						.findViewById(R.id.textView_chat_datetime);

				convertView.setTag(holderRight);

			} else {

				holderRight = (ViewHolderImageRight) convertView.getTag();

			}

			imageMsg.setChatImageView(convertView);

			imageLoader.displayImage(str_server + myInfo.getHeadSmall(),
					holderRight.img_imageView, options);

			imageLoader.displayImage(
					"file://" + imageMsg.getLocal_image_path(),
					holderRight.img_chatImage, options);

			holderRight.img_chatImage.setTag(imageMsg.getLocal_image_path());

			holderRight.img_chatImage.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO 自动生成的方法存根
					EntityUserPhoto userPhoto = new EntityUserPhoto();

					userPhoto.setAttuid(imageMsg.getImage());

					userPhoto.setPhobig(imageMsg.getLocal_image_path());

					userPhoto.setPhosmall(imageMsg.getImageThumbSrc());

					startImagePagerActivity(userPhoto);

				}
			});

			if (mt.getDateDiff(message.getMessage().getDatetime(),
					CurrentDateTime) >= 10) {
				holderRight.txt_chatDatetime.setVisibility(View.VISIBLE);
				holderRight.txt_chatDatetime.setText(message.getMessage()
						.getDatetime());
			} else {
				holderRight.txt_chatDatetime.setVisibility(View.GONE);
				holderRight.txt_chatDatetime.setText("");
			}

			break;

		case HiChatMessage.CHATMESSAGE_PERSON:

			if ((convertView == null)
					|| !(convertView.getTag() instanceof ViewHolderImageLeft)) {

				convertView = LayoutInflater.from(mContext).inflate(
						R.layout.item_chat_left_image, parent, false);

				holderLeft = new ViewHolderImageLeft();

				holderLeft.img_chatImage = (ImageView) convertView
						.findViewById(R.id.img_chat_image_left);

				holderLeft.img_imageView = (ImageView) convertView
						.findViewById(R.id.img_chat_photo_left);

				holderLeft.txt_chatDatetime = (TextView) convertView
						.findViewById(R.id.textView_chat_datetime);

				convertView.setTag(holderLeft);

			} else {

				holderLeft = (ViewHolderImageLeft) convertView.getTag();

			}

			imageLoader.displayImage(str_server + userFriend.getUfPhoto(),
					holderLeft.img_imageView, options);

			if (mt.getDateDiff(message.getMessage().getDatetime(),
					CurrentDateTime) >= 10) {
				holderLeft.txt_chatDatetime.setVisibility(View.VISIBLE);
				holderLeft.txt_chatDatetime.setText(message.getMessage()
						.getDatetime());
			} else {
				holderLeft.txt_chatDatetime.setVisibility(View.GONE);
				holderLeft.txt_chatDatetime.setText("");
			}

			// 去下载网络图片

			imageMsg.setChatImageView(convertView);

			imageMsg.imageShow();

			// ImgHandler getImagehandler = new ImgHandler(holderLeft);
			//
			// MyTools.getInstance().downLoadFileByAttuid(imageMsg.getImage(),
			// getImagehandler);

			// }

			break;
		}

		return convertView;
	}

	private class ImgHandler extends Handler {

		ViewHolderImageLeft holderLeft;

		public ImgHandler(ViewHolderImageLeft holderLeft) {
			// TODO 自动生成的构造函数存根
			this.holderLeft = holderLeft;
		}

		@Override
		public void handleMessage(Message msg) {
			// TODO 自动生成的方法存根
			super.handleMessage(msg);

			JsonObject jsReturn = mt.getJsonDownloadReturn().getAsJsonObject();

			if (jsReturn.get("state").getAsString().equals("200")) {

				String small = jsReturn.get("body").getAsJsonArray().get(0)
						.getAsJsonObject().get("phosmall").getAsString();

				final String big = jsReturn.get("body").getAsJsonArray().get(0)
						.getAsJsonObject().get("phobig").getAsString();

				Log.i(TAG + "_" + small, small);

				imageLoader.displayImage(str_server + small,
						this.holderLeft.img_chatImage, options);

				this.holderLeft.img_chatImage
						.setOnClickListener(new OnClickListener() {

							@Override
							public void onClick(View v) {
								// TODO 自动生成的方法存根
								// TODO 自动生成的方法存根
								EntityUserPhoto userPhoto = new EntityUserPhoto();

								userPhoto.setAttuid("");

								userPhoto.setPhobig(str_server + big);

								userPhoto.setPhosmall("");

								startImagePagerActivity(userPhoto);
							}
						});
			}

		}

	}

	private View displayVoiceMessage(int position, View convertView,
			ViewGroup parent, HiChatMessage message) {
		// TODO 自动生成的方法存根
		ViewHolderVoiceLeft holderLeft = null;
		ViewHolderVoiceRight holderRight = null;

		int msgtype = message.getWhoseMessage();

		switch (msgtype) {
		case HiChatMessage.CHATMESSAGE_MINE:
			if ((convertView == null)
					|| !(convertView.getTag() instanceof ViewHolderVoiceRight)) {
				convertView = LayoutInflater.from(mContext).inflate(
						R.layout.item_chat_right_voice, parent, false);

				holderRight = new ViewHolderVoiceRight();

				holderRight.txt_message = (TextView) convertView
						.findViewById(R.id.txt_chat_voice_right);
				holderRight.img_imageView = (ImageView) convertView
						.findViewById(R.id.img_chat_photo_right);
				holderRight.txt_chatDatetime = (TextView) convertView
						.findViewById(R.id.textView_chat_datetime);
				holderRight.textView_seconds = (TextView) convertView
						.findViewById(R.id.textView_seconds);

				convertView.setTag(holderRight);

			} else {
				holderRight = (ViewHolderVoiceRight) convertView.getTag();
			}

			holderRight.txt_message.setText("");
			holderRight.txt_message.setCompoundDrawablesWithIntrinsicBounds(0,
					0, R.drawable.voiceright, 0);

			// 语音长度
			EntityMsgVoice mg = (EntityMsgVoice) message.getMessage();

			holderRight.txt_message.setText("");

			holderRight.textView_seconds.setText(mg.getVoice_length() + "\"");

			Resources res1 = mContext.getResources();
			Drawable myImage1 = res.getDrawable(R.drawable.voiceright);
			myImage1.setBounds(0, 0, 32, 32);
			holderRight.txt_message.setCompoundDrawables(null, null, myImage1,
					null);

			float w = mg.getVoice_length() / 60.0f;

			w = w >= 1 ? 1 : w;

			int width1 = 100 + (int) ((MainApplication.getInstance()
					.getScreenWidth() * 1 / 2) * w);

			LayoutParams params1 = new LayoutParams(width1,
					LayoutParams.WRAP_CONTENT);

			params1.addRule(RelativeLayout.LEFT_OF, R.id.img_chat_photo_right);
			params1.setMargins(0, 0, 10, 0);

			holderRight.txt_message.setLayoutParams(params1);
			// 语音长度

			holderRight.txt_message.setTag(message.getMessage());

			holderRight.txt_message.setOnClickListener(voiceClickListener);

			imageLoader.displayImage(str_server + myInfo.getHeadSmall(),
					holderRight.img_imageView, options);
			if (mt.getDateDiff(message.getMessage().getDatetime(),
					CurrentDateTime) >= 10) {
				holderRight.txt_chatDatetime.setVisibility(View.VISIBLE);
				holderRight.txt_chatDatetime.setText(message.getMessage()
						.getDatetime());
			} else {
				holderRight.txt_chatDatetime.setVisibility(View.GONE);
				holderRight.txt_chatDatetime.setText("");
			}
			break;

		case HiChatMessage.CHATMESSAGE_PERSON:
			if ((convertView == null)
					|| !(convertView.getTag() instanceof ViewHolderVoiceLeft)) {
				convertView = LayoutInflater.from(mContext).inflate(
						R.layout.item_chat_left_voice, parent, false);

				holderLeft = new ViewHolderVoiceLeft();

				holderLeft.txt_message = (TextView) convertView
						.findViewById(R.id.txt_chat_voice_left);

				holderLeft.img_imageView = (ImageView) convertView
						.findViewById(R.id.img_chat_photo_left);

				holderLeft.txt_chatDatetime = (TextView) convertView
						.findViewById(R.id.textView_chat_datetime);

				holderLeft.textView_seconds = (TextView) convertView
						.findViewById(R.id.textView_seconds);

				convertView.setTag(holderLeft);
			} else {
				holderLeft = (ViewHolderVoiceLeft) convertView.getTag();
			}

			// 语音长度
			EntityMsgVoice mg2 = (EntityMsgVoice) message.getMessage();

			holderLeft.txt_message.setText("");
			// holderLeft.txt_message.setTextSize(TypedValue.COMPLEX_UNIT_SP,
			// 10);
			holderLeft.textView_seconds.setText(mg2.getVoice_length() + "\"");

			Resources res = mContext.getResources();
			Drawable myImage = res.getDrawable(R.drawable.voiceleft);
			myImage.setBounds(0, 0, 32, 32);
			holderLeft.txt_message.setCompoundDrawables(myImage, null, null,
					null);

			// holderLeft.txt_message.setCompoundDrawablesWithIntrinsicBounds(
			// R.drawable.voiceleft, 0, 0, 0);

			float w2 = mg2.getVoice_length() / 60.0f;

			w2 = w2 >= 1 ? 1 : w2;

			int width2 = 100 + (int) ((MainApplication.getInstance()
					.getScreenWidth() * 1 / 2) * w2);

			LayoutParams params2 = new LayoutParams(width2,
					LayoutParams.WRAP_CONTENT);

			params2.addRule(RelativeLayout.RIGHT_OF, R.id.img_chat_photo_left);
			params2.setMargins(10, 0, 0, 0);

			holderLeft.txt_message.setLayoutParams(params2);
			// 语音长度

			holderLeft.txt_message.setTag(message.getMessage());

			holderLeft.txt_message.setOnClickListener(voiceClickListener);
			if (mt.getDateDiff(message.getMessage().getDatetime(),
					CurrentDateTime) >= 10) {
				holderLeft.txt_chatDatetime.setVisibility(View.VISIBLE);
				holderLeft.txt_chatDatetime.setText(message.getMessage()
						.getDatetime());
			} else {
				holderLeft.txt_chatDatetime.setVisibility(View.GONE);
				holderLeft.txt_chatDatetime.setText("");
			}
			imageLoader.displayImage(str_server + userFriend.getUfPhoto(),
					holderLeft.img_imageView, options);

			break;
		}
		return convertView;
	}

	private View displayGifMessage(int position, View convertView,
			ViewGroup parent, HiChatMessage message) {
		// TODO 自动生成的方法存根

		ViewHolderGifRight holderGifRight = null;

		ViewHolderGifLeft holderGifLeft = null;

		int msgtype = message.getWhoseMessage();
		GifDrawable gifDrawable;

		switch (msgtype) {
		case HiChatMessage.CHATMESSAGE_MINE:

			if ((convertView == null)
					|| !(convertView.getTag() instanceof ViewHolderGifRight)) {
				convertView = LayoutInflater.from(mContext).inflate(
						R.layout.item_chat_right_gif, parent, false);

				holderGifRight = new ViewHolderGifRight();
				holderGifRight.img_gif = (GifImageView) convertView
						.findViewById(R.id.img_chat_gif_right);
				holderGifRight.img_imageView = (ImageView) convertView
						.findViewById(R.id.img_chat_photo_right);
				holderGifRight.txt_chatDatetime = (TextView) convertView
						.findViewById(R.id.textView_chat_datetime);
				convertView.setTag(holderGifRight);
			} else {
				holderGifRight = (ViewHolderGifRight) convertView.getTag();
			}

			try {
				gifDrawable = new GifDrawable(res, res.getIdentifier(
						((EntityMsgGif) message.getMessage()).getGif(), "raw",
						packetName));

				holderGifRight.img_gif.setImageDrawable(gifDrawable);

			} catch (NotFoundException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			} catch (IOException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}

			imageLoader.displayImage(str_server + myInfo.getHeadSmall(),
					holderGifRight.img_imageView, options);
			if (mt.getDateDiff(message.getMessage().getDatetime(),
					CurrentDateTime) >= 10) {
				holderGifRight.txt_chatDatetime.setVisibility(View.VISIBLE);
				holderGifRight.txt_chatDatetime.setText(message.getMessage()
						.getDatetime());
			} else {
				holderGifRight.txt_chatDatetime.setVisibility(View.GONE);
				holderGifRight.txt_chatDatetime.setText("");
			}
			break;

		case HiChatMessage.CHATMESSAGE_PERSON:
			if ((convertView == null)
					|| !(convertView.getTag() instanceof ViewHolderGifLeft)) {
				convertView = LayoutInflater.from(mContext).inflate(
						R.layout.item_chat_left_gif, parent, false);

				holderGifLeft = new ViewHolderGifLeft();
				holderGifLeft.img_gif = (GifImageView) convertView
						.findViewById(R.id.img_chat_gif_left);
				holderGifLeft.img_imageView = (ImageView) convertView
						.findViewById(R.id.img_chat_photo_left);
				holderGifLeft.txt_chatDatetime = (TextView) convertView
						.findViewById(R.id.textView_chat_datetime);
				convertView.setTag(holderGifLeft);
			} else {
				holderGifLeft = (ViewHolderGifLeft) convertView.getTag();
			}

			try {
				gifDrawable = new GifDrawable(res, res.getIdentifier(
						((EntityMsgGif) message.getMessage()).getGif(), "raw",
						packetName));

				holderGifLeft.img_gif.setImageDrawable(gifDrawable);

			} catch (NotFoundException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			} catch (IOException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
			if (mt.getDateDiff(message.getMessage().getDatetime(),
					CurrentDateTime) >= 10) {
				holderGifLeft.txt_chatDatetime.setVisibility(View.VISIBLE);
				holderGifLeft.txt_chatDatetime.setText(message.getMessage()
						.getDatetime());
			} else {
				holderGifLeft.txt_chatDatetime.setVisibility(View.GONE);
				holderGifLeft.txt_chatDatetime.setText("");
			}
			imageLoader.displayImage(str_server + userFriend.getUfPhoto(),
					holderGifLeft.img_imageView, options);

			break;

		}

		return convertView;
	}

	private View displayTextMessage(int position, View convertView,
			ViewGroup parent, HiChatMessage message) {
		// TODO 自动生成的方法存根
		ViewHolderLeft holderLeft = null;
		ViewHolderRight holderRight = null;

		int msgtype = message.getWhoseMessage();

		switch (msgtype) {

		case HiChatMessage.CHATMESSAGE_MINE:
			if ((convertView == null)
					|| !(convertView.getTag() instanceof ViewHolderRight)) {
				convertView = LayoutInflater.from(mContext).inflate(
						R.layout.item_chat_right, parent, false);

				holderRight = new ViewHolderRight();
				holderRight.txt_message = (TextView) convertView
						.findViewById(R.id.txt_chat_msg_right);
				holderRight.img_imageView = (ImageView) convertView
						.findViewById(R.id.img_chat_photo_right);
				holderRight.txt_chatDatetime = (TextView) convertView
						.findViewById(R.id.textView_chat_datetime);
				convertView.setTag(holderRight);
			} else {
				holderRight = (ViewHolderRight) convertView.getTag();

			}

			break;
		case HiChatMessage.CHATMESSAGE_PERSON:
			if ((convertView == null)
					|| !(convertView.getTag() instanceof ViewHolderLeft)) {
				convertView = LayoutInflater.from(mContext).inflate(
						R.layout.item_chat_left, parent, false);

				holderLeft = new ViewHolderLeft();
				holderLeft.txt_message = (TextView) convertView
						.findViewById(R.id.txt_chat_msg_left);
				holderLeft.img_imageView = (ImageView) convertView
						.findViewById(R.id.img_chat_photo_left);
				holderLeft.txt_chatDatetime = (TextView) convertView
						.findViewById(R.id.textView_chat_datetime);
				convertView.setTag(holderLeft);
			} else {
				holderLeft = (ViewHolderLeft) convertView.getTag();
			}

			break;
		}

		switch (msgtype) {
		case HiChatMessage.CHATMESSAGE_MINE:
			holderRight.txt_message.setText(MyTools
					.convertNormalStringToSpannableString(mContext, message
							.getMessage().getBody(), false));
			imageLoader.displayImage(str_server + myInfo.getHeadSmall(),
					holderRight.img_imageView, options);

			if (mt.getDateDiff(message.getMessage().getDatetime(),
					CurrentDateTime) >= 10) {
				holderRight.txt_chatDatetime.setVisibility(View.VISIBLE);
				holderRight.txt_chatDatetime.setText(message.getMessage()
						.getDatetime());

			} else {
				holderRight.txt_chatDatetime.setVisibility(View.GONE);
				holderRight.txt_chatDatetime.setText("");
			}
			break;

		case HiChatMessage.CHATMESSAGE_PERSON:
			holderLeft.txt_message.setText(MyTools
					.convertNormalStringToSpannableString(mContext, message
							.getMessage().getBody(), false));
			imageLoader.displayImage(str_server + userFriend.getUfPhoto(),
					holderLeft.img_imageView, options);
			if (mt.getDateDiff(message.getMessage().getDatetime(),
					CurrentDateTime) >= 10) {

				holderLeft.txt_chatDatetime.setVisibility(View.VISIBLE);
				holderLeft.txt_chatDatetime.setText(message.getMessage()
						.getDatetime());

			} else {
				holderLeft.txt_chatDatetime.setVisibility(View.GONE);
				holderLeft.txt_chatDatetime.setText("");
			}
			break;
		}

		return convertView;
	}

	private static class ViewHolderLeft {
		TextView txt_message;
		ImageView img_imageView;
		TextView txt_chatDatetime;
	}

	private static class ViewHolderRight {
		TextView txt_message;
		ImageView img_imageView;
		TextView txt_chatDatetime;
	}

	private static class ViewHolderVoiceLeft {
		TextView txt_message;
		ImageView img_imageView;
		TextView txt_chatDatetime;
		TextView textView_seconds;
	}

	private static class ViewHolderVoiceRight {
		TextView txt_message;
		ImageView img_imageView;
		TextView txt_chatDatetime;
		TextView textView_seconds;
	}

	private static class ViewHolderGifRight {

		GifImageView img_gif;
		ImageView img_imageView;
		TextView txt_chatDatetime;
	}

	private static class ViewHolderGifLeft {

		GifImageView img_gif;
		ImageView img_imageView;
		TextView txt_chatDatetime;
	}

	private static class ViewHolderImageLeft {

		ImageView img_imageView;
		ImageView img_chatImage;
		TextView txt_chatDatetime;

	}

	private static class ViewHolderImageRight {
		ImageView img_imageView;
		ImageView img_chatImage;
		TextView txt_chatDatetime;
	}

	@Override
	public long getItemId(int position) {
		// Unimplemented, because we aren't using Sqlite.
		return position;
	}

	private OnClickListener voiceClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO 自动生成的方法存根
			final EntityMsgVoice mg = (EntityMsgVoice) v.getTag();

			String local_voice = mg.getLocal_voice_path();
			String voice = mg.getVoice();

			File voiceFile = new File(local_voice);

			if (voiceFile.exists()) {
				L.d(TAG, "文件存在时的BODY：" + mg.getAllMessage());
				L.i(TAG, "播放本地文件");
				MyTools.getInstance().playMusic(local_voice);
			} else {
				// TODO:以后要完善
				L.d(TAG, "文件不存在时的BODY：" + mg.getAllMessage());
				L.i(TAG, "去下载服务器文件，然后播放");
				final MyTools mT = MyTools.getInstance();
				Handler getVoiceHandler = new Handler() {

					@Override
					public void handleMessage(Message msg) {
						// TODO 自动生成的方法存根
						super.handleMessage(msg);
						JsonElement jsonReturn = mT.getJsonDownloadReturn();
						JsonObject jO = jsonReturn.getAsJsonObject();
						if (jO.get("state").getAsString().equals("200")) {

							final String str_localFile = MainApplication
									.getInstance().getAppVoiceDirectory()
									+ "/"
									+ mg.getyUID()
									+ "__"
									+ System.currentTimeMillis() + ".amr";

							String str_serverFile = jO.get("body")
									.getAsJsonArray().get(0).getAsJsonObject()
									.get("filesrc").getAsString();

							Handler downloadFileHandler = new Handler() {

								@Override
								public void handleMessage(Message msg) {
									// TODO 自动生成的方法存根
									super.handleMessage(msg);

									String allMessage = mg.getAllMessage();

									int int_begin = allMessage
											.indexOf("<local_voice_path>");

									int int_end = allMessage
											.indexOf("</local_voice_path>");

									String allMsg1 = allMessage.substring(0,
											int_begin + 18);
									String allMsg2 = allMessage.substring(
											int_end, allMessage.length());

									String newMsg = allMsg1 + str_localFile
											+ allMsg2;

									ChatDAO.getInstence().changeNewMessageBody(
											mg.getMessageID(), newMsg);

									L.d("allMsg1", allMsg1);
									L.d("allMsg2", allMsg2);
								}

							};

							mT.downLoadFileByFilePath(str_serverFile, 1,
									str_localFile, downloadFileHandler);

						}
					}

				};
				mT.downLoadFileByAttuid(voice, getVoiceHandler);

			}
		}
	};

	private void startImagePagerActivity(EntityUserPhoto userPhoto) {

		Intent intent = new Intent();

		intent.setClass(this.mContext, ActivityStandardImage.class);

		intent.putExtra("image", userPhoto.getPhobig());

		this.mContext.startActivity(intent);

	}
}
